<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@page import="com.framework.entity.Global"%>
<%@ page import="com.biz.utils.ConfigUtil" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
    String resoursUrl = ConfigUtil.get("RESOURCES_URL");
%>
<!DOCTYPE html>
<html class="no-js">
<head>
  <meta charset="UTF-8">
  <title >申请退款</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="format-detection" content="telephone=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <jsp:include page="/jsp/inc_head.jsp"></jsp:include>
  <script src="assets/js/TouchSlide.1.1.source.js"></script>
</head>
<body>
<section class="am-g am-text-sm">
    <div class="am-u-sm-12 sqtkDdxx" id="orderInfo">
    	<input type="hidden"  id="picid" "> 
    </div>
    <div class="fenge"></div>
    <div class="am-u-sm-12 sqtkListDiv">
    	<div class="am-u-sm-3 am-padding-0 margt2">
    		退款金额
        </div>
        <div class="am-u-sm-9 am-padding-0">
        	<input type="text" id="refundMoney" placeholder="点击输入金额">
            <span class="posit2">元</span>
           <!-- <span class="am-text-xs am-fr">（最高可退 <span style="color:#ff6060;">￥128.00</span>）</span>-->
        </div>
    </div>
    <div class="am-u-sm-12 sqtkListDiv" id="userInfo">
   
    </div>
    <div class="am-u-sm-12 sqtkListDiv">
    	<div class="am-u-sm-12 am-padding-0">
    		备注
        </div>
        <div class="am-u-sm-12 am-padding-0">
        	<textarea placeholder="请填写内容" id="refundNote"></textarea>
        </div>
    </div>    
    <div class="am-u-sm-12">
        <div class="am-u-sm-12 wdzhPeople">
            图片
        </div>
        <div class="am-u-sm-12 grzlShdz">
        	<ul id="doc-scrollspy-wrapper" style="margin-bottom:0;padding:0;">
              <li class="fileInputContainer am-sqshym-gdfrsty" id="f1">      
                  <input type="file" class="fileInput" id="fileToUpload1" name="fileToUpload" onChange="uploadFile('fileToUpload1','f1','picId1');"/>
                  <input type="hidden" id="picId1">
                  <img src="<%=resoursUrl%>resources/assets/images/imgdel.png" class="imgdel" onClick="imgdel(this,1)" />
              </li>
            </ul>
            <div class="am-sqshym-gdfrsty" id="doc-scrollspy-insert">
            	<img src="<%=resoursUrl%>resources/assets/images/sqshplus.png" class="sqshplus" />
            </div>
        </div>
        <div class="am-u-sm-12 tksqDbTips">（最多上传3张图片）</div>
    </div>
</section>

<!--footer-->
<div class="am-g gdDbdiv"></div>
<a  class="footeNavbar botBtn" onclick="subRefund()">提交</a>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn">确定</span>
		</div>
	</div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert2">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title2"></div>
		<div class="am-modal-bd" id="alert_message2"></div>
		<div class="am-modal-footer" id="alert_button2">
			 
		</div>
	</div>
</div>
<script>
var ajaxpath="${pageContext.request.contextPath}";
var file_num=1;
var picIds=[];
var detailIds="${orderId}";
var patTotal=0;
var type='${type}';
var refundId='';
$(function() {
loadOrderInfo();
loadUserInfo();
if(type=='1'||type=='2')
{loadRefund();}
  var $wrapper = $('#doc-scrollspy-wrapper');
  var $appendPanel = function() {
  file_num++;
   //  var panel = '<li class="fileInputContainer am-sqshym-gdfrsty"><input type="file" class="fileInput"/><img src="<%=resoursUrl%>resources/assets/images/imgdel.png" class="imgdel" onClick="imgdel(this)" /></li>';
    var panel = '<li class="fileInputContainer am-sqshym-gdfrsty" id="f'+file_num+'"> <input type="hidden" id="picId'+file_num+'"><input type="file" class="fileInput"  id="fileToUpload'+file_num+'" name="fileToUpload" onChange="uploadFile(\'fileToUpload'+file_num+'\',\'f'+file_num+'\',\'picId'+file_num+'\');" /><img src="<%=resoursUrl%>resources/assets/images/imgdel.png" class="imgdel" onClick="imgdel(this,\''+file_num+'\')" /></li>';
   
     $wrapper.append(panel);
  };
  $('#doc-scrollspy-insert').on('click', function() {
     $appendPanel();
  });
});
function appendPic()
{var $wrapper = $('#doc-scrollspy-wrapper');
file_num++;
var panel = '<li class="fileInputContainer am-sqshym-gdfrsty" id="f'+file_num+'"> <input type="hidden" id="picId'+file_num+'"><input type="file" class="fileInput"  id="fileToUpload'+file_num+'" name="fileToUpload" onChange="uploadFile(\'fileToUpload'+file_num+'\',\'f'+file_num+'\',\'picId'+file_num+'\');" /><img src="<%=resoursUrl%>resources/assets/images/imgdel.png" class="imgdel" onClick="imgdel(this,\''+file_num+'\')" /></li>';
$wrapper.append(panel);
}
function imgdel(obj,id){
var delId=$("#picId"+id).val();
picIds.pop(delId);
/* alert(picIds); */
	$(obj).parent().remove();
}

function loadRefund()
{
refundId='${refundId}';
var url=ajaxpath+'/order/getOrderInfoForDetail.do?id=${refundId}';
 	$.get(url, function(result){
var data=JSON.parse(result);
$("#refundMoney").val(data.money);
$("#refundNote").val(data.note);
$('#doc-scrollspy-wrapper').html("");
file_num=0;
	$(data.pics).each(function(i){
	appendPic();
	$("#f"+file_num).css("background","url("+picPath_goods+data.pics[i].path+") no-repeat 0 0/100% 100%"); 
           $("#picId"+file_num).val(data.pics[i].id);
            picIds.push(data.pics[i].id);
	});

});
}
function loadOrderInfo()
{

 var url=ajaxpath+'/order/getOrderInfo.do?orderId=${orderId}';
 
 	$.get(url, function(result){ 	
	
	var data=eval(result);
	var orderCode=data[0].CODE;	
	var goodsName=[];
	var payTime=data[0].payTime;
	var weekDay="";
	switch (data[0].payTimeWeek) {
	case '0':
		weekDay="周日";
		break;
	case '1':
		weekDay="周一";
		break;
	case '2':
		weekDay="周二";
		break;
	case '3':
		weekDay="周三";
		break;
	case '4':
		weekDay="周四";
		break;
	case '5':
		weekDay="周五";
		break;
	case '6':
		weekDay="周六";
		break;
	default:
		break;
	}
	$(data).each(function(i){
	goodsName.push(data[i].goodsName);
// 	detailIds.push(data[i].detailId);
	patTotal=toDecimal2(accAdd(patTotal,data[i].payTotal));
	});
	var html='<div class="am-u-sm-12 am-padding-0">'+
	'订单编号  '+orderCode+
	'</div>'+
	'<div class="am-u-sm-12 am-padding-0">'+
	'商品名称 '+goodsName+
	'</div>'+
	'<div class="am-u-sm-12 am-padding-0">'+
	'订单金额  <span class="sqtkDdjeNum">￥'+patTotal+'</span>'+
	'</div>'+
	'<div class="am-u-sm-12 am-padding-0">'+
	'付款时间 '+payTime+'  '+weekDay+
	' </div>';
       $("#orderInfo").html(html);
 	}); 
}
function loadUserInfo()
{
var url=ajaxpath+'/userCenter/queryMyInfo.do?orderId=${orderId}';
 	$.get(url, function(result){
 	var html='姓名&nbsp;&nbsp;'+
 	'<span>'+result.personName+'</span>'+
    '&nbsp;&nbsp;&nbsp;&nbsp;'  +
    ' 手机号&nbsp;&nbsp;' +
    '<span>' +result.phone+'</span>';
$("#userInfo").html(html);
 	});
}

function accAdd(arg1,arg2){  //浮点数运算加法
    var r1,r2,m;  
    try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}  
    try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}  
    m=Math.pow(10,Math.max(r1,r2))  ;
    return (arg1*m+arg2*m)/m  ;
}

function toDecimal2(x) {  
            var f = parseFloat(x);  
            if (isNaN(f)) {  
                return false;  
            }  
            var f = Math.round(x*100)/100;  
            var s = f.toString();  
            var rs = s.indexOf('.');  
            if (rs < 0) {  
                rs = s.length;  
                s += '.';  
            }  
            while (s.length <= rs + 2) {  
                s += '0';  
            }  
            return s;  
        }
        
        
        
var picPath_goods='<%=Global.OSSURL%>/';
function uploadFile(obj,id,picId){
	//var detailId = $("#detailId").val();
	//$("#"+id).css("background","url(${pageContext.request.contextPath}/resources/assets/images/waiting.gif) no-repeat 0 0/100% 100%");
    $("#"+id).css("background","url(${pageContext.request.contextPath}/resources/assets/image/loading.gif) no-repeat 0 0/100% 100%");
    $.ajaxFileUpload({
           url: '${pageContext.request.contextPath}/order/doUploadFile.do',  
           secureuri: false,  
           type:'post',
           fileElementId: obj,  
           dataType: 'text',  
           success: function(data, status) {  
//         	  alert(data);
        	   var img=JSON.parse(data);
        	      $("#"+id).css("background","url("+picPath_goods+img.path+") no-repeat 0 0/100% 100%"); 
           $("#"+picId).val(img.picId);
            picIds.push(img.picId);
             // var picIds=$("#picId").val();
             // $("#picId").val(picIds+picId+",");
           },  
           error: function(data, status, e) {  
              alert(e);
               $("#"+id).css("background","url(${pageContext.request.contextPath}/resources/assets/images/sqshplus.png) no-repeat 0 0/100% 100%");

           }  
       });  
}

function subRefund()
{
var money=$("#refundMoney").val();
var note=$("#refundNote").val();
if(money==null||money==''||money==undefined)
{
	 $("#alert_message").html("请填写退款金额！");
	$("#my-alert").modal('open');

}
else
{
if(parseFloat(money)>parseFloat(patTotal))
{ $("#alert_message").html("退款金额不得大于订单金额！");
	$("#my-alert").modal('open');
}else if(parseFloat(money)<=parseFloat(0))
{ $("#alert_message").html("退款金额需要大于0元！");
    $("#my-alert").modal('open');
}
else
{
	note=encodeURIComponent(note);
	var json='{"refundId":"'+refundId+'", "type":"'+type+'","ids":"'+detailIds+'","picIds":"'+picIds+'","money":"'+money+'","note":"'+note+'"}';
	var url=ajaxpath+'/order/addOrderRefund.do?json='+json;
	$.ajax({
             type: "post",
             url: url,
             data: json,
             dataType: "json",
             success: function(res){            
             if(res.flag=="success")
             {
             	 $("#alert_message2").html("申请成功！");
			
				var html= '<span class="am-modal-btn" onclick="toDetail(\''+res.id+'\')">确定</span>';
				 $("#alert_button2").html(html);
					$("#my-alert2").modal('open');
             }
             else
             {	
              $("#alert_message").html(res.flag);
				$("#my-alert").modal('open');
             }
                       }
         });
}
	
}


}

function toDetail(id)
{
	 window.location.href = ajaxpath+"/order/toOrderRefundDetail.do?id="+detailIds+"&refund="+id+"&type=0";
    
}
</script>
</body>
</html>


